*******************************************************************************
*This file calculates mutual fund LPI
*******************************************************************************

*set working directory
global folder ".."


global HY 0 // HY=1/5 means that proxy for loan liquidity using HY bonds, 0 is the baseline using implied illiquidity measure
global Muni = 0 // Muni=0 means using IRC, Muni = 1 means using implied illiquidity 


use "$folder/data_input/mf_prelmi_cleaned_2011_2017.dta", clear
	
*-------------------------------------------------------------------------------------------------*
* Use HY bonds as proxy for loan liquidity
*-------------------------------------------------------------------------------------------------*
if $HY>0 { 
merge m:1 yq using "$folder/data_input/corpbond_IRC_HY"
drop if _m==2
drop _m

local list com_loan 
foreach x of local list{
	replace awgt_`x'= 1 - corpbond_IRC_HY*$HY
}
}

*-------------------------------------------------------------------------------------------------*
* Use Implied illiquidity as proxy for muni liquidity
*-------------------------------------------------------------------------------------------------*
if $Muni==1 { 
local list muni 
foreach x of local list{
	replace awgt_`x'= 1 - (1 - awgt_bond) * 2
}
}
********************************************************************************
* generate cumulative portfolio weight  (the weight upto J asset)
g temp_weight = 0
local list cash treas ag_deb ag_mbscmo com_loan mm muni bond cmoabs eq fasset
foreach x of local list {
g `x'_weight = a_mf_`x'/assetstotal
g `x'_weight_cum = temp_weight + `x'_weight 
replace temp_weight = temp_weight + `x'_weight 
}
drop temp_weight

* keep outflow only
g flow_trunc = flow
replace flow_trunc = 0 if flow>0
replace flow_trunc = -1 if flow<-1


*-------------------------------------------------------------------------------------------------*
* Construct LPI using illiquidity measures 
*-------------------------------------------------------------------------------------------------*
	
	
	local list eq bond treas muni
	foreach x of local list{
		replace awgt_`x' = 0 if missing(awgt_`x')
	}
	
	* generate cumulative liquidation value (the liquidation value upto J asset)
	g temp = 0
	local list cash treas ag_deb ag_mbscmo com_loan mm muni bond cmoabs eq fasset
	foreach x of local list {
	g `x'_liq_value_pct = `x'_weight*awgt_`x'
	g `x'_liq_value_pct_cum = temp + `x'_liq_value_pct
	replace temp = temp +  `x'_liq_value_pct
	}
	rename temp liq_value_pct
	
	* 1. LPI without swing pricing
	gen lpi = 0 if -flow_trunc>liq_value_pct
	replace lpi = 1/liq_value_pct-1 if -flow_trunc<liq_value_pct

	gen expected_lti = lpi // create the same variable names
	sum expected_lti, d
	
	label var lpi "LPI without swing pricing"
		
	* 2. LPI with partial NAV striking

	foreach num of numlist 0/10 {
		local mu=`num'/10
		g lpi_`num' = .

		local list cash treas ag_deb ag_mbscmo com_loan mm muni bond cmoabs eq fasset // ranked by average liquidity
		foreach x of local list {
		replace lpi_`num' = (`x'_liq_value_pct_cum-`x'_liq_value_pct+ (1-(`x'_weight_cum-`x'_weight))*awgt_`x'+(1-`mu')*(-(`x'_liq_value_pct_cum-`x'_liq_value_pct)+(`x'_weight_cum-`x'_weight)*awgt_`x'))/liq_value_pct/(1-(1-`mu'*(-flow_trunc))*(1-awgt_`x'))-1 ///
		if -flow_trunc>=(`x'_liq_value_pct_cum-`x'_liq_value_pct)/(1+`mu'*((`x'_liq_value_pct_cum-`x'_liq_value_pct)-(`x'_weight_cum-`x'_weight)))
		}

	
		label var lpi_`num' "LPI with `mu' NAV striking"
	}
	
	
	winsor2 lpi*, cut(1 99) replace
	
	
	* 3. this section create lpi_JLW 
	merge m:1 year quarter using "$folder/data_input/VIX"
	drop if _m==2
	drop _m
	g expected_lti_JLW=expected_lti
	replace expected_lti_JLW=(1.457/4.430+2.402/3.741+(-2.503/-4.941)+(0/-1.947)+(-0.632/-2.570))/4*expected_lti if high==1 //Table 4 Panel A from Jiang Li Wang. 
	
		
	if $HY>0 {
	save "$folder/data_intermediate/fund_ltisample_haircutrobust_$HY.dta", replace
	}
	if $HY==0 {
	save "$folder/data_intermediate/fund_ltisample_haircutrobust_new.dta", replace
	}
	if $Muni==1 {
	save "$folder/data_intermediate/fund_ltisample_haircutrobust_Muni.dta", replace
	}


